package crp.services.api.tasks;

import crp.core.resource.app.ResourceService;
import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.ScheduledExecution;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Optional;

@ApplicationScoped
public class SyncResourceTask implements Scheduled.SkipPredicate {
    private final ResourceService resourceService;
    private final boolean enabled;

    @Inject
    public SyncResourceTask(ResourceService resourceService,
                            @ConfigProperty(name = "app.task.sync-resource-task.enabled") Optional<Boolean> enabled) {
        this.resourceService = resourceService;
        this.enabled = enabled.orElse(false);
    }

    @Scheduled(
            cron = "{app.task.sync-resource-task.cron}",
            skipExecutionIf = SyncResourceTask.class
    )
    public void execute() {
        resourceService.sync();
    }

    @Override
    public boolean test(ScheduledExecution execution) {
        return !enabled;
    }
}
